use crate::config::create_data_item_service;
use crate::utils;
use utils::{convert_json_to_table, export_excel};

#[tauri::command]
pub async fn export_xlsx_command(
    after: &str,
    before: &str,
    name: &str,
    path: &str,
) -> Result<String, String> {
    let data_item_service = create_data_item_service();
    let data = match data_item_service.get_data_items_by_time_range(after, before, name) {
        Ok(data) => data,
        Err(e) => return Err(e.to_string()),
    };
    let (headers, rows) = match convert_json_to_table(&data) {
        Some(table) => table,
        None => return Err("Failed to convert json to table".to_string()),
    };
    export_excel(headers, rows, path).unwrap();
    Ok("success".to_string())
}
